package com.logistics.team3.common;

import java.util.List;

public class Page<T> {
	private int beginIndex;// 数字页码开始时的索引
	private int endIndex;// 结束处的索引
	private int firstPage;// 第一页
	private boolean hasFirst;// 是否有第一页
	private boolean hasLast;// 是否有最后一页
	private int lastPage;// 最后一页
	private List<T> lists;// 保存数据到集合
	private int nextPage;// 下一页
	private int pageIndex = 1;// 当前页码
	private int pageSize = 10;// 每页大小
	private int prevPage;// 上一页
	private int rowCount;// 总记录
	private int totalPages;// 总页数
	private String url;// 请求地址

	public Page() {

	}


	public Page(int pageIndex, int pageSize, int rowCount) {
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.rowCount = rowCount;
		this.init();
	}


	public Page(int pageIndex, int pageSize, int rowCount, String url) {
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.rowCount = rowCount;
		this.url = url;
		this.init();
	}
	/**
	 * 初始化page    
	 * 根据pageIndex，pageSize，rowCount
	 * 给每一个属性赋值
	 */
	public void init() {
		int temp = rowCount / pageSize;
		this.totalPages = rowCount % pageSize == 0 ? temp : temp + 1;
		if (pageIndex <= 1) {
			this.pageIndex = 1;
			this.prevPage = 1;
		} else {
			this.prevPage = pageIndex - 1;
		}

		if (pageIndex >= totalPages) {
			if(totalPages!=0){
				this.pageIndex = totalPages;
			}
			this.nextPage = totalPages;
		} else {
			this.nextPage = pageIndex + 1;
		}

		this.firstPage = 1;
		this.lastPage = totalPages;

		if (pageIndex <= 1) {
			hasFirst = true;
		} else {
			hasFirst = false;
		}

		if (pageIndex >= totalPages) {
			hasLast = true;
		} else {
			hasLast = false;
		}

		if (totalPages <= 10 || pageIndex + 4 >= totalPages) {
			endIndex = totalPages;
		} else if (totalPages >= 10 && pageIndex < 6) {
			endIndex = 10;
		} else {
			endIndex = pageIndex + 4;
		}

		if (pageIndex - 5 <= 1) {
			beginIndex = 1;
		} else if (endIndex == totalPages) {
			if (totalPages - 9 <= 1) {
				beginIndex = 1;
			} else {
				beginIndex = totalPages - 9;
			}
		} else {
			beginIndex = pageIndex - 5;
		}
	}


	public int getBeginIndex() {
		return beginIndex;
	}


	public int getEndIndex() {
		return endIndex;
	}


	public int getFirstPage() {
		return firstPage;
	}


	public int getLastPage() {
		return lastPage;
	}


	public List<T> getLists() {
		return lists;
	}


	public int getNextPage() {
		return nextPage;
	}


	public int getPageIndex() {
		return pageIndex;
	}


	public int getPageSize() {
		return pageSize;
	}


	public int getPrevPage() {
		return prevPage;
	}


	public int getRowCount() {
		return rowCount;
	}


	public int getTotalPages() {
		return totalPages;
	}


	public String getUrl() {
		return url;
	}



	public boolean isHasFirst() {
		return hasFirst;
	}


	public boolean isHasLast() {
		return hasLast;
	}


	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}


	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}


	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}


	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}


	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}


	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}


	public void setLists(List<T> lists) {
		this.lists = lists;
	}


	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}


	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}


	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}


	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Page [pageIndex=" + pageIndex + ", pageSize=" + pageSize
				+ ", rowCount=" + rowCount + ", totalPages=" + totalPages
				+ ", prevPage=" + prevPage + ", nextPage=" + nextPage
				+ ", firstPage=" + firstPage + ", lastPage=" + lastPage
				+ ", hasFirst=" + hasFirst + ", hasLast=" + hasLast
				+ ", lists=" + lists + ", beginIndex=" + beginIndex
				+ ", endIndex=" + endIndex + ", url=" + url + "]";
	}


}
